﻿using System.Data.Entity;
using KPIS.GERP.Models;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.HRM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web.Mvc;
using System.Linq.Dynamic;

namespace KPIS.GERP.WCF.GHRMS
{
    [ServiceContract]
    public interface IEmployeeService
    {
        [OperationContract]
        IEnumerable<Employee> Select(Employee filter, string[] includeSubObjectString, bool? bIsActive);

        [OperationContract]
        List<SelectListItem> EmployeesSelectList(Employee criteriaModel, bool? bIsActive);
    }

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "EmployeeService" in code, svc and config file together.
	// NOTE: In order to launch WCF Test Client for testing this service, please select EmployeeService.svc or EmployeeService.svc.cs at the Solution Explorer and start debugging.
	[ServiceErrorBehavior(typeof(ElmahErrorHandler))]
    public class EmployeeService : IEmployeeService
    {
        GERPContext db = new GERPContext(false);

        public IEnumerable<Employee> Select(Employee criteriaModel, string[] includeSubObjectString, bool? bIsActive = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    //IQueryable<Employee> query = context.Employees.Include(o=>o.MasNamePrefix);
                    IQueryable<Employee> query = context.Employees;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    if (bIsActive.HasValue)
                    {
                        query.Where(m => m.MasEmployeeStatus.Active == (bIsActive.Value ? "1" : "0"));
                    }

                    var result = query .ToList();

                    return result;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public List<SelectListItem> EmployeesSelectList(Employee criteriaModel, bool? bIsActive = null)
        {
            var data = new List<SelectListItem>();

            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<Employee> query = context.Employees.Include("MasEmployeeStatus");

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    if (bIsActive.HasValue)
                    {
                        query.Where(m => m.MasEmployeeStatus.Active == (bIsActive.Value ? "1" : "0"));
                    }
                    var models = query.ToList();

                    foreach (var Employee in models)
                    {
                        var item = new SelectListItem { Text = Employee.FullName, Value = Employee.EmpId.ToString() };
                        //if (Employee.EmpId == id) item.Selected = true;
                        data.Add(item);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return data;
        }
    }
}
